package com.wanjia.seal.manager.controller;

import com.alibaba.fastjson.JSONObject;
import com.wanjia.seal.manager.annotation.LoginToken;
import com.wanjia.seal.manager.controller.ex.DataNotFoundException;
import com.wanjia.seal.manager.controller.ex.userexistException;
import com.wanjia.seal.manager.entity.codetb;
import com.wanjia.seal.manager.mapper.CodeTBMapper;
import com.wanjia.seal.manager.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/api")
@Api(tags = "代码信息母表管理")
public class CodeTbController extends BaseController{

    @Autowired
    CodeTBMapper codeTBMapper;

    @ResponseBody
    @PostMapping("/code")
    @ApiOperation(value = "添加母表类型", notes = "添加是否成功")
    @LoginToken
    public JsonResult createCodeTB(@RequestBody codetb codetb){
        Integer codeID = codetb.getCodeID();
        String codeName = codetb.getCodeName();
        String reserved = codetb.getReserved();
        codetb result = codeTBMapper.findByCodeID(codeID);
        if (result!=null){
            throw new DataNotFoundException("数据不存在的异常");
        }
        codetb.setCodeID(codeID);
        codetb.setCodeName(codeName);
        codetb.setReserved(reserved);
        codeTBMapper.createByCode(codetb);
        return new JsonResult(OK);
    }


    @ResponseBody
    @GetMapping("/code/id")
    @ApiOperation(value = "ID查询母表类型", notes = "ID查询是否成功")
    @LoginToken
    public JsonResult<codetb> selectCodeTB(Integer CodeID){
        codetb result = codeTBMapper.findByCodeID(CodeID);
        /*if (result==null){
            throw new DataNotFoundException("数据不存在的异常");
        }*/
        return new JsonResult<>(OK,result);
    }

    @ResponseBody
    @GetMapping("/codes")
    @ApiOperation(value = "查询所有母表类型", notes = "查询是否成功")
    @LoginToken
    public JsonResult<Object> selectsCodeTB(Integer pageNum, Integer pageSize){
        List<codetb> result = codeTBMapper.findByCode(pageNum,pageSize);
        for (codetb s : result) {
            System.out.println(s);
        }
        Integer rows = codeTBMapper.findByCodeTOTAL();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total",rows);
        jsonObject.put("codetb",result);
        return new JsonResult<>(OK,jsonObject);
    }

    @ResponseBody
    @PutMapping("/code/id")
    @ApiOperation(value = "更新母表类型", notes = "更新是否成功")
    @LoginToken
    public JsonResult updateCodeTB(@RequestBody codetb codetb){
        Integer CodeID = codetb.getCodeID();
        codetb result = codeTBMapper.findByCodeID(CodeID);
        if (result==null){
            throw new DataNotFoundException("数据不存在的异常");
        }
        String NewCodeName = codetb.getCodeName();
        String NewReserved = codetb.getReserved();
        codeTBMapper.updateByCode(CodeID, NewCodeName, NewReserved);
        return new JsonResult(OK);
    }

    @ResponseBody
    @DeleteMapping("/code/id")
    @ApiOperation(value = "删除母表类型", notes = "删除是否成功")
    @LoginToken
    public JsonResult DeleteCodeTB(Integer CodeID){
        codetb result = codeTBMapper.findByCodeID(CodeID);
        if (result==null){
            throw new DataNotFoundException("数据不存在的异常");
        }
        Integer total = codeTBMapper.findByCodeTypeTOTAL(CodeID);
        if (total > 1){
            throw new userexistException("有数据存在");
        }
        codeTBMapper.deleteByCode(CodeID);
        return new JsonResult(OK);
    }
}
